package com.hyts.fscan.tools;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.DirectoryStream.Filter;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

import lombok.Data;

/**
 * @author libo
 * @date 2019-12-08
 * 支持的相关扩展工具类
 * 针对于扫描功能的提供着
 */
public class NFileScanHelper {
		
	/**
	 * 获取相关此目录下面的所有文件信息集合
	 * @param path
	 * @return
	 */
	public static List<File> getFiles(String path){
		List<File> fileList = null;
		if(StringUtils.isNotEmpty(path)) {
			try {
				fileList = Files.list(Paths.get(path)).map(param->param.toFile())
						.collect(Collectors.toList());
			} catch (IOException e) {
				return fileList;
			}
		}
		return fileList;
	}
		
	/**
	 * 文件扫描器操作服务提供工具访问者模式
	 * @author libo
	 *
	 */
	public static class FileScanVistor extends SimpleFileVisitor<Path>{
		
		private List<File> fileList;
		
		private FileFilter filter;
		
		/**
		 * 操作访问服务访问类
		 * @param fileList
		 */
		public FileScanVistor(List<File> fileList) {
			super();
			this.fileList = fileList;
		}

		
		public FileScanVistor(List<File> fileList, FileFilter filter) {
			super();
			this.fileList = fileList;
			this.filter = filter;
		}

		/**
		 * 后置扫描处理操作
		 */
		@Override
		public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
			return super.postVisitDirectory(dir, exc);
		}
		
		/**
		 * 前置扫描处理操作
		 */
		@Override
		public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
			return super.preVisitDirectory(dir, attrs);
		}
		
		/**
		 * 处理服务工具访问类
		 */
		@Override
		public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
			File fileSubject = file.toFile();
			if(Objects.isNull(filter)) {
				fileList.add(fileSubject);
			}else {
				if(filter.accept(fileSubject)) {
				  fileList.add(fileSubject);
				}
			}
			return FileVisitResult.CONTINUE;
		}
		
		/**
		 * 访问服务工具文件失败！
		 */
		@Override
		public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
			// TODO Auto-generated method stub
			return super.visitFileFailed(file, exc);
		}
	}
	
	
	public static List<File> staticFiles(String path,FileFilter filter){
		List<File> fileList = new ArrayList<>();
		try {
			Files.walkFileTree(Paths.get(path), new FileScanVistor(fileList,filter));
		} catch (IOException e) {
			return fileList;
		}
		return fileList;
	}
	
	
	public static List<File> scanFiles(String path,Filter<Path> filter){
		List<File> fileList = new LinkedList<>();
		if(StringUtils.isNoneEmpty(path)) {
			try(DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(path),filter);) {
				stream.forEach(param->{
					fileList.add(param.toFile());
				});				
			} catch (IOException e) {
				return fileList;
			}
		}
		return fileList;
	}
	
	public static List<File> scanFiles(String path){
		List<File> fileList = new LinkedList<>();
		if(StringUtils.isNoneEmpty(path)) {
			try(DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(path));) {
				stream.forEach(param->{
					fileList.add(param.toFile());
				});				
			} catch (IOException e) {
				return fileList;
			}
		}
		return fileList;
	}
	
	
	
}
